
#include "view.h"
#include "view_def.h"
#include "view_font.h"

#ifdef VIEW_ENABLE_3D
#include "3d_window.h"
#endif

#include <string.h>

int32_t View::FontLoad(ViewFontType font, const char* fontFile)
{
    return ViewFont::GetVF()->Load(font, fontFile);
}

int32_t View::FontUnLoad()
{
    return ViewFont::GetVF()->UnLoad();
}

void View::Default()
{
    m_shapeX = 0;
    m_shapeY = 0;
    m_shapeWidth = 0;
    m_shapeHeight = 0;

    m_textXErr = 0;
    m_textYErr = 0;
    m_textWidth = 0;
    m_textHeight = 0;

    m_infoCrc32 = 0;
    m_resizeFlag = 0;
}

View::View()
{
    Default();
}

View::View(const ViewInfo& info)
{
    Default();
    m_info = info;
}

View::~View()
{
    ;
}

int32_t View::Render(Image* screenImage, int32_t x, int32_t y)
{
    if (screenImage)
    {
        m_imageGrid = ImageGrid(
            *screenImage,
            x, y,
            screenImage->Width() - x,
            screenImage->Height() - y);
    }

    return OnCallback(VCT_DRAW);
}

/* ----- 参数查询 ----- */

int32_t View::GetX()
{
    return m_shapeX;
}

int32_t View::GetY()
{
    return m_shapeY;
}

int32_t View::GetWidth()
{
    return m_shapeWidth;
}

int32_t View::GetHeight()
{
    return m_shapeHeight;
}

ImageGrid& View::GetImageGrid()
{
    return m_imageGrid;
}

uint32_t View::GetResizeFlag()
{
    return m_resizeFlag;
}

/* ----- 链表控制和查询 ----- */

int32_t View::ChildInsert(const ViewInfo& info, int32_t index)
{
    InsertChild(new View(info), index);
    return 0;
}

int32_t View::ChildInsert(View* view, int32_t index)
{
    InsertChild(view, index);
    return 0;
}

int32_t View::ChildAdd(const ViewInfo& info, bool front)
{
    if (front)
        PushFrontChild(new View(info));
    else
        PushBackChild(new View(info));
    return 0;
}

int32_t View::ChildAdd(View* view, bool front)
{
    if (front)
        PushFrontChild(view);
    else
        PushBackChild(view);
    return 0;
}

int32_t View::ChildRemove(int32_t index)
{
    return 0;
}

int32_t View::ChildSwap(int32_t index1, int32_t index2)
{
    return 0;
}

View* View::Parent()
{
    return (View*)CrossList::Parent();
}

View* View::Prev()
{
    return (View*)CrossList::Prev();
}

View* View::Next()
{
    return (View*)CrossList::Next();
}

int32_t View::Index()
{
    return CrossList::Index();
}

View* View::At(int32_t index)
{
    return (View*)CrossList::At(index);
}

int32_t View::ChildNum()
{
    return CrossList::ChildNum();
}

View* View::ChildBegin()
{
    return (View*)CrossList::ChildBegin();
}

View* View::ChildEnd()
{
    return (View*)CrossList::ChildEnd();
}

View* View::ChildAt(int32_t index)
{
    return (View*)CrossList::ChildAt(index);
}

int32_t View::OnCallback(ViewCallbackType type, int32_t x, int32_t y)
{
    /* 事件分发 */
    if (type < VCT_ZOOM_IN)
        return CallbackClick(type, x, y);
    else if (type < VCT_DISPLAY)
        return CallbackZoom(type, x, y);
    else if (type < VCT_RELEASE)
        return CallbackState(type);
    else if (type < VCT_TOTAL)
        return CallbackRelease(type);
    UI_ERR("unknown ViewCallbackType %d", (int32_t)type);
    return -1;
}
